﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace PresensiClient.util
{
    public class Configuration
    {
        private static String _KODE;
        private static String defaultSetting;

        Configuration() { }

        public static String KODE
        {
            get { return _KODE; }
            set { _KODE = value; }
        }

        public static String DefaultSetting
        {
            get { return getDefaultSetting(); }
            set { defaultSetting = value; }
        }

        private static String getDefaultSetting()
        {
            if (defaultSetting == null)
            {
                defaultSetting = Environment.CurrentDirectory + "\\" + "config.xml";
            }

            return defaultSetting;
        }

        public static void read(String path)
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
            xmlDoc.Load(path); //* load the XML document from the specified file.

            //* Get elements.
            XmlNodeList client = xmlDoc.GetElementsByTagName("client");

            _KODE = client[0].ChildNodes[0].InnerText.ToString();
        }

        public static void write(String path)
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
            xmlDoc.Load(path);

            XmlNode node;
            node = xmlDoc.DocumentElement;

            foreach (XmlNode node1 in node.ChildNodes)
            {
                foreach (XmlNode node2 in node1.ChildNodes)
                {
                    if (node1.Name.Equals("client"))
                    {
                        if (node2.Name.Equals("kode"))
                        {
                            node2.InnerText = _KODE;
                        }
                    }
                }
            }

            xmlDoc.Save(path);
        }
    }
}
